/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/


#ifndef MK_HTTP_UTIL_H
#define MK_HTTP_UTIL_H
#include "MkUtil/MkTypes.h"

typedef enum _status_code
{
    MkHttpStateInvalid = 0,
    MkHttpStateContinue = 100,
    MkHttpStateSwitchProtocol = 101,
    MkHttpStateOk = 200,
    MkHttpStateCreated = 201,
    MkHttpStateAccpeted = 202,
    MkHttpStateNonAuthoritative = 203,
    MkHttpStateNoContent = 204,
    MkHttpStateResetContent = 205,
    MkHttpStatePartialContent = 206,
    MkHttpStateMultipleChoices = 300,
    MkHttpStateMovedPermanently = 301,
    MkHttpStateFound = 302,
    MkHttpStateSeeOther = 303,
    MkHttpStateNotModified = 304,
    MkHttpStateUseProxy = 305,
    MkHttpStateTemporaryRedirect = 307,
    MkHttpStateBadRequest = 400,
    MkHttpStateUnauthorized = 401,
    MkHttpStatePaymentRequired = 402,
    MkHttpStateForbidden = 403,
    MkHttpStateNotFound = 404,
    MkHttpStateMethodNotAllowed = 405,
    MkHttpStateNotAcceptable = 406,
    MkHttpStateProxyAuthentication = 407,
    MkHttpStateTimeout = 408,
    MkHttpStateConflict = 409,
    MkHttpStateGone = 410,
    MkHttpStateLengthRequired = 411,
    MkHttpStatePreconditionFailed = 412,
    MkHttpStateEntityTooLarge = 413,
    MkHttpStateURITooLarge = 414,
    MkHttpStateUnsupportCodec = 415,
    MkHttpStateRangeNotSatisfiable = 416,
    MkHttpStateExpectationFailed = 417,
    MkHttpStateInternalServerError = 500,
    MkHttpStateNotImplemented = 501,
    MkHttpStateBadGateway = 502,
    MkHttpStateServiceUnavailable = 503,
    MkHttpStateGatewayTimeout = 504,
    MkHttpStateVersionUnsupported = 505,
}MkHttpStatusCode;

typedef enum _body_type
{
    MkHttpBodyTypeNone = 0x00,
    MkHttpBodyTypeJson = 0x01,
    MkHttpBodyTypeXml = 0x02,
    MkHttpBodyTypeText = 0x03,
    MkHttpBodyTypePdf = 0x04,
    MkHttpBodyTypeForm = 0x05,     //data from form data as key1=val1&key2=val2  form not  enctype 
    MkHttpBodyTypeFormData = 0x06,     //data from form enctyped
    MkHttpBodyTypeHtml = 0x07
}MkHttpBodyType;

//https://www.rfc-editor.org/rfc/rfc2616.html#section-5.1.1
typedef enum _method_type
{
    MkHttpMethodUnknown = 0x00,
    MkHttpMethodOptions = 0x01,
    MkHttpMethodGet = 0x02,
    MkHttpMethodHead = 0x03,
    MkHttpMethodPost = 0x04,
    MkHttpMethodPut = 0x05,
    MkHttpMethodDelete = 0x06,
    MkHttpMethodTrace = 0x07,
    MkHttpMethodConnect = 0x08
}MkHttpMethod;

#define WsUuid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

class MkApi CMkHttpUtil
{
public:
    ~CMkHttpUtil();
    static CMkHttpUtil& GetInstance() { return m_Instance; }

    const MkString& GetCodeDesc(const MkHttpStatusCode& StatusCode) const;
    const MkString& GetBodyTypeDesc(const MkHttpBodyType& BodyType) const;
    MkHttpMethod GetMkHttpMethod(const MkString& Method) const;
    const MkString& GetMethod(const MkHttpMethod& Method) const;


    static Uint32 HttpUrlParse(const MkString &HttpUrl, MkString& Domain, MkString &Host, Uint16 &Port, MkString& Path);
    static Uint32 WebsocketUrlParse(const MkString &HttpUrl, MkString& Domain, MkString &Host, Uint16 &Port, MkString& Path);
protected:
private:
    CMkHttpUtil();
private:
    static CMkHttpUtil                              m_Instance;

    MkUnorderedMap<Uint32, MkString>                m_MapCodeDesc;
    MkUnorderedMap<Uint32, MkString>                m_MapBodyType;
    MkUnorderedMap<Uint32, MkString>                m_MapMethod;
    MkUnorderedMap<MkString, MkHttpMethod>          m_MapMkMethod;
    MkString                                        m_EmptyString;
};


#endif